home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-10-10 | 9.4 KB | 252 lines | [TEXT/MPS ] |
- unit ThreadsIntf;
-
- interface
-
- const
-
- kDefaultStackSize = 1024;
- kUsesFPU = 1;
- kMainThreadHasPriority = 2;
- kDreamEveryTick = 4;
-
- { Thread errors }
-
- threadsNotInitedErr = -2700;
- badThreadErr = -2701;
- threadsNotAvailableErr = -2702;
- wrongThreadsVersErr = -2702;
- badSemaphoreErr = -2703;
- threadQueueNotEmptyErr = -2704;
- badSwapSelectorErr = -2705;
- noOtherThreadsToRunErr = -2706;
- kSemaphoreNotFoundErr = -2712;
-
- { Customizable swapping behavior instalation selectors }
-
- kCopyContextSel = 'cctx';
- kSwapInSel = 'swpi';
- kSwapOutSel = 'swpo';
- kFreeThreadSel = 'tfre';
- kScheduleSel = 'schd';
- kPreYieldSel = 'prey';
- kPostYieldSel = 'post';
-
- { Thread States }
-
- running = 0; { the Thread is the active context }
- pending = 1; { the Thread is awaiting scheduleing ( ie . on gPendingQueue ) }
- blocked = 2; { the Thread is on a semaphore }
- sleeping = 3; { the Thread is not blocked or pending }
- ended = 4; { the Thread is about to be free 'd }
-
- kNumberOfUserLongs = 8;
- kNumberOfInternalLongs = 4;
-
- { ----------------------------------------------------------- }
-
- type
-
- { Each thing can be a member of a list , and can also contain a list of other things . }
-
- ThingHandle = ^ThingPtr;
- ThingPtr = ^Thing;
- Thing = record
- fQueue: ThingHandle; { the containing “Thing list” this thing is on }
- fNext: ThingHandle; { the next thing in the list relative to this thing }
- fPrev: ThingHandle; { the previous thing in the list relative to this thing }
- fHead: ThingHandle; { the first thing in the list of things owned by this thing }
- fTail: ThingHandle; { the last thing in the list of things owned by this thing }
- end;
-
- ThreadType = longInt;
- ThreadState = longInt;
- ThreadQueueHandle = ^ThreadQueuePtr;
- ThreadQueuePtr = ^ThreadQueue;
- ThreadQueue = ptr;
- ThingIterProc = ProcPtr;
- ThreadIterProc = ProcPtr;
-
-
- ThreadProc = ProcPtr;
- ScheduleProc = ProcPtr;
- SpawnProc = ProcPtr;
-
- ThreadProcTbl = record
- fCopyContext: ThreadProc; { copy current context - store in fStack }
- fSwapIn: ThreadProc; { called to context switch "this" thread in }
- fSwapOut: ThreadProc; { calls fSchedule then fSwapIn on the nextThread }
- fFree: ThreadProc; { called to dispose of the Thread }
- fSchedule: ScheduleProc; { queue this thread (if necessary), return the next one }
- fPreYield: ThreadProc;
- fPostYield: ThreadProc;
- end;
-
- ThreadHandle = ^ThreadPtr;
- ThreadPtr = ^Thread;
- Thread = record
- fThing: Thing; { linked list for queue thread is on }
- fNext: ThreadHandle; { linked list of all threads }
- fPrev: ThreadHandle;
- fUserBytes: array[1..8] of longInt; { for user use }
- fInternalUse: Ptr; { used to keep track of internal globals }
- fThreadIdentifier: longInt; { used for error checking }
- fDream: ThreadProc; { idle proc that gets called for each thread when LetThreadsDream is called }
- fThreadProcs: ThreadProcTbl; { customizable routines }
- fType: ThreadType;
- fState: ThreadState;
- fLocked: boolean; { If the ThreadHandle and fStack are locked - avoids copious calls to HLock/HUnlock }
- fStack: Handle; { the storage for the stack data - if ThreadType == HeapStack this is HLock'ed }
- end;
-
- SemaphoreHandle = ^SemaphorePtr;
- SemaphorePtr = ^Semaphore;
- Semaphore = record
- fThing: Thing;
- fCount: longInt;
- fSemaphoreGID: longInt;
- fNext: SemaphoreHandle;
- fPrev: SemaphoreHandle;
- fSemaphoreIdentifier: longInt;
- end;
-
- { ----------------------------------------------------------- }
-
- var
-
- {$J+}
- gThreadError: OSErr;
- {$J-}
-
- { ----------------------------------------------------------- }
-
- { Manager Routines }
-
- function InitThreads(threadFlags: integer): OSErr;
- function ThreadError: OSErr;
- function InstallSwapProc(selector: longint; newSwap: ThreadProc): ThreadProc;
- function RegisterContextGlobal(var theGlobal: longInt): OSErr;
- function SetMainThread(mainThread: ThreadHandle): OSErr;
- function GetCurrentThread: ThreadHandle;
- procedure Yield;
- procedure ExitThreads; { while gPendingQueue is not empty Yield }
-
- { Thread Routines }
-
- function NewThread(stackSize: longInt): ThreadHandle;
- function StartThread(theThread: ThreadHandle): OSErr; { call fCopy proc and wake }
- function InThread(theThread: ThreadHandle): boolean; { true if theThread is running }
- function InMainThread: boolean; { true if mainThread is running }
- function Sleep(theThread: ThreadHandle): OSErr; { remove form gPendingQueue (or swap out if running) place on gSleepingQueue }
- function SleepForNTicks(theThread: ThreadHandle; sleepTime: longint): OSErr; { like sleep, but wake up after N ticks }
- function Wake(theThread: ThreadHandle): OSErr; { place on gPendingQueue }
- function EndThread(theThread: ThreadHandle): OSErr; { call fFree - never returns, end of context }
-
- { Convenience Routines }
-
- function InNewThread(var theThread: ThreadHandle; stackSize: longInt): boolean; { NewThread, StartThread, InThread }
- function Spawn(theThread: ThreadHandle; theSpawnProc: SpawnProc; stackSize, refCon: longInt): ThreadHandle;
-
- { Default methods }
-
- procedure TCopyContext(theThread: ThreadHandle); { copy the current context }
- procedure TSwapIn(theThread: ThreadHandle); { make this thread's context the active context }
- procedure TSwapOut(theThread: ThreadHandle); { save the current context (or free the thread if fState == ended ) }
- procedure TFree(theThread: ThreadHandle); { dispose of the thread }
- function TSchedule(theThread: ThreadHandle): ThreadHandle; { returns the next thread to be run, queue this if necessary }
-
- { Thing Routines }
-
- procedure IThing(theThing: ThingHandle);
- procedure ThingInsertLast(queue, thing: ThingHandle);
- function ThingTakeFirst(theThing: ThingHandle): ThingHandle;
- function ThingGetFirst(theThing: ThingHandle): ThingHandle;
- procedure ThingRemove(theThing: ThingHandle);
- function ThingOwner(theThing: ThingHandle): ThingHandle;
- function ThingIsEmpty(theThing: ThingHandle): boolean;
- procedure ThingEach(q: ThingHandle; proc: ThingIterProc; refCon: ptr);
- procedure FreeThing(theThing: ThingHandle);
-
- { Dreaming Support }
-
- procedure ThreadEach(theProc: ThreadIterProc; refCon: ptr);
- procedure LetThreadsDream;
-
- { Semaphores }
-
- function NewSemaphore: SemaphoreHandle;
- procedure BlockOnSemaphore(theSemaphore: SemaphoreHandle; theThread: ThreadHandle);
- procedure ReleaseOneThread(theSemaphore: SemaphoreHandle);
- procedure ReleaseAllThreads(theSemaphore: SemaphoreHandle);
- procedure FreeSemaphore(theSemaphore: SemaphoreHandle);
- procedure GrabSemaphore(theSemaphore: SemaphoreHandle);
- procedure ReleaseSemaphore(theSemaphore: SemaphoreHandle);
- function LookupSemaphore(semaphoreGID: longInt): SemaphoreHandle;
-
- { ----------------------------------------------------------- }
-
- implementation
-
- { Manager Routines }
-
- function InitThreads(threadFlags: integer): OSErr; external;
- function ThreadError: OSErr; external;
- function InstallSwapProc(selector: longint; newSwap: ThreadProc): ThreadProc; external;
- function RegisterContextGlobal(var theGlobal: longInt): OSErr; external;
- function SetMainThread(mainThread: ThreadHandle): OSErr; external;
- function GetCurrentThread: ThreadHandle; external;
- procedure Yield; external;
- procedure ExitThreads; external;
-
- { Thread Routines }
-
- function NewThread(stackSize: longInt): ThreadHandle; external;
- function StartThread(theThread: ThreadHandle): OSErr; external;
- function InThread(theThread: ThreadHandle): boolean; external;
- function InMainThread: boolean; external;
- function Sleep(theThread: ThreadHandle): OSErr; external;
- function SleepForNTicks(theThread: ThreadHandle; sleepTime: longint): OSErr; external;
- function Wake(theThread: ThreadHandle): OSErr; external;
- function EndThread(theThread: ThreadHandle): OSErr; external;
-
- { Convenience Routines }
-
- function InNewThread(var theThread: ThreadHandle; stackSize: longInt): boolean; external;
- function Spawn(theThread: ThreadHandle; theSpawnProc: SpawnProc; stackSize, refCon: longInt): ThreadHandle; external;
-
- { Default methods }
-
- procedure TCopyContext(theThread: ThreadHandle); external;
- procedure TSwapIn(theThread: ThreadHandle); external;
- procedure TSwapOut(theThread: ThreadHandle); external;
- procedure TFree(theThread: ThreadHandle); external;
- function TSchedule(theThread: ThreadHandle): ThreadHandle; external;
-
- { Thing Routines }
-
- procedure IThing(theThing: ThingHandle); external;
- procedure ThingInsertLast(queue, thing: ThingHandle); external;
- function ThingTakeFirst(theThing: ThingHandle): ThingHandle; external;
- function ThingGetFirst(theThing: ThingHandle): ThingHandle; external;
- procedure ThingRemove(theThing: ThingHandle); external;
- function ThingOwner(theThing: ThingHandle): ThingHandle; external;
- function ThingIsEmpty(theThing: ThingHandle): boolean; external;
- procedure ThingEach(q: ThingHandle; proc: ThingIterProc; refCon: ptr); external;
- procedure FreeThing(theThing: ThingHandle); external;
-
- { Dreaming Support }
-
- procedure ThreadEach(theProc: ThreadIterProc; refCon: ptr); external;
- procedure LetThreadsDream; external;
-
- { Semaphores }
-
- function NewSemaphore: SemaphoreHandle; external;
- procedure BlockOnSemaphore(theSemaphore: SemaphoreHandle; theThread: ThreadHandle); external;
- procedure ReleaseOneThread(theSemaphore: SemaphoreHandle); external;
- procedure ReleaseAllThreads(theSemaphore: SemaphoreHandle); external;
- procedure FreeSemaphore(theSemaphore: SemaphoreHandle); external;
- procedure GrabSemaphore(theSemaphore: SemaphoreHandle); external;
- procedure ReleaseSemaphore(theSemaphore: SemaphoreHandle); external;
- function LookupSemaphore(semaphoreGID: longInt): SemaphoreHandle; external;
-
- end.